<?php

    class mailsystemmodule {
        function name() { return 'Mailsystemmodule Module'; }
        function description() { return 'Mailsystemmodule module for mailing list management'; }
        function author() { return 'Sonhb'; }

        function hasSources() { return true; }
        function hasContent() { return false; }
        function hasViews() { return true; }

        function supportsWorkflow() { return false; }

        function permissions($internal = '') {
            return array(
            'administrate'=>"Administrate",
            'manage'=>"Manage Email",
            'mail'=>"Send mailling list",
            'delete'=>"Delete Email",
            );
        }

        function show($view,$loc = null, $title = '') {
            global $db;			
            $config=$db->selectObject("mailsystemmodule_config","location_data='".serialize($loc)."'");

            $template = new template('mailsystemmodule',$view,$loc);
            $template->register_permissions(array('administrate',"mail"),$loc);
            // Assign page data
            $template->assign('config', $config);
            $template->assign('moduletitle', $title);
            $template->output();
        }
        function install()
        {
            global $db;
            $sql_mail="CREATE TABLE IF NOT EXISTS {$db->prefix}maillinglist(
            email text,
            id int(10) NOT NULL AUTO_INCREMENT,
            users_id int(11) DEFAULT NULL,
            blacklist int(1) DEFAULT '0',
            email_id int(11) DEFAULT NULL,
            PRIMARY KEY(id)
            )";
            @mysqli_query($db->connection,$sql_mail);
        }
        function deleteIn($loc) {
            return false;
        }

        function copyContent($oloc,$nloc) {
            global $db;
            return false;
        }

        function searchName() {
            return false;
        }

        function spiderContent($item = null) {
            return false;
        }
        function logMail($type,$email,$description)
        {
            global $db;
            $log=null;
            $log->type=$type;
            $log->date=time();
            $log->email=$email;
            $log->description=$description;
            $db->insertObject($log,'mailsystem_log');
        }
        

        function SendMail($subject, $content, $format=0, $receive, $dont_add_admin=false,$from_mail,$from,$group='default'){
            global $db;
            $mail=null;
            $email_id=mailsystemmodule::check_email($_REQUEST['_mail_email_to']);
            if($email_id)
            {                         
                $mail->subject=$subject;
                $mail->content=$content;
                $mail->mail_type=$format;                         
                $mail->email_to=$receive;
                $mail->email_from=$from_mail;
                $mail->email_from_name=$from;
                $mail->date_insert=time();
                $mail->group=$group;  
                $content->date=time();   
                $db->insertObject($mail,'mailsystem_mail');    
                //Log 
                mailsystemmodule::logMail('insertmail',$mail->email_to,"Insert \"{$mail->subject}\" to queues");
                return true;
            }
        }

        function check_email($email)
        {
            $email=trim($email);
            global $db;
            //Jan 3, 2013 - sonhb - get_detail email 
            $email_detail=$db->selectObject("mailsystem_email","email='".$email."'");
            if($email_detail->block)
                return false;

            if(!$email_detail)
            {
                $email_detail->email=$email;
                $email_detail->date=time();
                mailsystemmodule::logMail('addemail',$email,"Add {$email} to list email");
                return $db->insertObject($email_detail,'mailsystem_email');          
            }

            return $email_detail->id;
            //Jan 3, 2013 - sonhb - get_detail email - end
        }

    }


?>
